<?php

/**
 * agenda actions.
 *
 * @package    sinc
 * @subpackage agenda
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12474 2008-10-31 10:41:27Z fabien $
 */
class agendaActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
  include('C:\wamp\www\sinc\plugins\sfPHPCalendarPlugin-0.9.0\lib\vendor\PHPCalendar-v2.9\calendar.inc.php');

  $annee = date('Y');
  $mois = date('n');

  $this->mois = $request->getParameter('mois');
  $this->annee = $request->getParameter('annee');

  if($this->mois != '')
     $mois = $request->getParameter('mois');

  if($this->annee != '')
     $annee = $request->getParameter('annee');
  
  if($mois <= 0){
    $mois += 12;
    $annee--;
  }
  else if($mois > 12){
    $mois -= 12;
    $annee++;
  }

  $this->id_user = $this->getUser()->getGuardUser()->getId();

  $this->id_agenda = AgendaPeer::doSelectAgenda($this->id_user);
  
  foreach( $this->id_agenda as $agenda2){
        $this->agenda = $agenda2->getIdAgenda();
    }

   $c = new Criteria();
   $c->setDistinct();
   $c->add(ContientPeer::ID_AGENDA,$this->agenda);
   $this->evenements = ContientPeer::doSelect($c);

   $c = new Criteria();
   $c->add(PagePeer::ID_USER,$this->id_user);
   $this->css = PagePeer::doSelect($c);

  $cal = new PHPCalendar($annee, $mois,$this->evenements);
  $cal->link = 'evenement/new';
  $this->output = $cal->create() . '<br>';
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new ContientForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod('post'));

    $this->form = new ContientForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($contient = ContientPeer::retrieveByPk($request->getParameter('id_agenda'),
                           $request->getParameter('id_evenement')), sprintf('Object contient does not exist (%s).', $request->getParameter('id_agenda'),
                           $request->getParameter('id_evenement')));
    $this->form = new ContientForm($contient);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod('post') || $request->isMethod('put'));
    $this->forward404Unless($contient = ContientPeer::retrieveByPk($request->getParameter('id_agenda'),
                           $request->getParameter('id_evenement')), sprintf('Object contient does not exist (%s).', $request->getParameter('id_agenda'),
                           $request->getParameter('id_evenement')));
    $this->form = new ContientForm($contient);

    $this->processForm($request, $this->form);
    $this->setTemplate('edit');    
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($contient = ContientPeer::retrieveByPk($request->getParameter('id_agenda'),
                           $request->getParameter('id_evenement')), sprintf('Object contient does not exist (%s).', $request->getParameter('id_agenda'),
                           $request->getParameter('id_evenement')));
    $contient->delete();

    $this->redirect('agenda/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $contient = $form->save();

      $this->redirect('agenda/index');
    }
  }

  public function executeEnregistrement(sfWebRequest $request)
  {
    $this->id_user = $this->getUser()->getGuardUser()->getId();

    $this->id_agenda = AgendaPeer::doSelectAgenda($this->id_user);

    $contient = new Contient();

    foreach( $this->id_agenda as $agenda){
        $contient->setIdAgenda($agenda->getIdAgenda());
    }
    
    $contient->setIdEvenement($request->getParameter('id_evenement'));

    $this->form = new ContientForm($contient);

    $this->processForm($request, $this->form);

  }
}
